package com.learn.springlearn.controller;

import cn.hutool.core.img.ImgUtil;
import com.learn.springlearn.config.SpringContextUtil;
import com.learn.springlearn.dao.UserMapper;
import com.learn.springlearn.entity.Couple;
import com.learn.springlearn.entity.User;
import com.learn.springlearn.entity.UserRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    UserMapper userMapper;

    @Value("#{'smp'}")
    String name;

    @Value("${phone:123}")
    Long phone;

    @Resource
    private RedisTemplate redisTemplate;


    @GetMapping("get")
    public User getUser(){
//        Map<String, UserMapper> beansOfType = SpringContextUtil.getApplicationContext().getBeansOfType(UserMapper.class);
//        System.out.println(SpringContextUtil.getApplicationContext().containsBean("userMapper"));
//        User user1 = (User) redisTemplate.opsForValue().get("user");
        User user = userMapper.selectByPrimaryKey(0);
        return user;
    }

    @GetMapping("set")
    public User set(){
        User user = new User();
        user.setName(name);
        user.setPhone(phone);
        redisTemplate.opsForValue().set("user",user);
        return user;
    }

    @PostMapping("save")
    public void save(@RequestBody UserRequest request) throws Exception {
        User user = new User();
        user.setName("time");
        user.setPhone(12L);
        user.setCreateTime(request.getCreateTime());

        int i = userMapper.insertSelective(user);
        System.out.println(user.getId());

    }




}
